package com.perfect.service;

import com.perfect.base.BaseService;
import com.perfect.dao.ResolveOrMapper;
import com.perfect.utils.AssertUtil;
import com.perfect.vo.ResolveOr;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author YU
 */
@Service
public class ResolveOrService extends BaseService<ResolveOr, Integer> {

    @Resource
    private ResolveOrMapper resolveOrMapper;


    @Transactional(propagation = Propagation.REQUIRED)
    public void updateContent(Integer id, String content){
        AssertUtil.isTrue(checkParamsIsBlank(content),"参数为空");
        AssertUtil.isTrue(selectByPrimaryKey(id) == null,"待处理退货单不存在");
        AssertUtil.isTrue(resolveOrMapper.updateContentById(id, content) < 1,"备注添加失败");
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void updateState(Integer id, String state){
        AssertUtil.isTrue(checkParamsIsBlank(state),"参数为空");
        AssertUtil.isTrue(selectByPrimaryKey(id) == null,"待处理退货单不存在");
        AssertUtil.isTrue(resolveOrMapper.updateStateById(id, state) < 1,"退货单处理失败");
    }

    /**
     * 删除订单
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteResolveOr(Integer id){
        AssertUtil.isTrue(checkParamsIsBlank(id),"请选择要删除的订单");
        AssertUtil.isTrue(selectByPrimaryKey(id) == null,"待删除记录不存在");
        AssertUtil.isTrue(deleteByPrimaryKey(id) < 1,"删除订单失败");
    }

    /**
     * 删除多条记录
     * @param ids
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteMoreResolveOr(String ids){
        AssertUtil.isTrue(checkParamsIsBlank(ids),"请选择要删除的订单");
        int count = 0;
        String[] id = ids.split(",");
        for (String i : id){
            deleteResolveOr(Integer.parseInt(i));
            count++;
        }
        AssertUtil.isTrue(id.length < count,"删除订单失败");
    }

    /**
     * 参数的非空判断
     * @param params
     * @return
     */
    public boolean checkParamsIsBlank(Object ...params){
        for (Object o : params){
            if (o == null){
                return true;
            }
            if (o instanceof String){
                if (o == null || o == ""){
                    return true;
                }
            }
        }
        return false;
    }
}
